package com.example.szh.fragment.viewmodel;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.example.mvvmcommon.mvvm.viewmodel.BaseViewModel;
import com.example.networkmoudel.entity.BaseEntity;
import com.example.szh.fragment.model.FragmentModel;
import com.example.szh.fragment.widget.BottomButton;
import com.example.szh.splash.data.ConfigEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class FragmentViewModel extends BaseViewModel<FragmentModel> implements BottomButton.SelectListener {




    public ObservableField<List<ConfigEntity.DataBean>> bottomes;

    @Override
    protected void initData() {
        bottomes=new ObservableField<>();
        Map<String,Object>map=new HashMap<>();
        map.put("imie", ""+System.currentTimeMillis());
        m.requestConfig(map).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(this);

    }

    @Override
    protected FragmentModel createModel() {
        return new FragmentModel();
    }

    @Override
    protected void sucessFull(BaseEntity entity) {
        if (entity instanceof ConfigEntity){
            ConfigEntity entity1 = (ConfigEntity) entity;
            entity1.getData().get(0).setSelFlag(true);
            bottomes.set(entity1.getData());
            LogUtils.json(entity1);
            getUiChangeLiveData().getSelFragment().setValue(entity1.getData().get(0).getArouterURL());
        }
    }

    @Override
    public void onSelect(int id) {
        List<ConfigEntity.DataBean> dataBeans = new ArrayList(bottomes.get());
        for (int i = 0; i < dataBeans.size(); i++) {
            if (id==i){
                dataBeans.get(i).setSelFlag(true);
                getUiChangeLiveData().getSelFragment().setValue(dataBeans.get(i).getArouterURL());
            }else {
                dataBeans.get(i).setSelFlag(false);
            }
        }
        bottomes.set(dataBeans);
    }
}
